﻿Imports System.Reflection
Imports Microsoft.Office.Interop

Public Class Form1



    Private Sub tmrMailCheck_Tick(sender As System.Object, e As System.EventArgs) Handles tmrMailCheck.Tick
        ' Create Outlook application.
        Dim oApp As Outlook.Application = New Outlook.Application()
        Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")

        ' Get Mapi NameSpace.
        Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
        oNS.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get Messages collection of Inbox.
        Dim oInbox As Outlook.MAPIFolder = oNS.Folders("Facilities Helpdesk").Folders("Inbox")
        Dim oItems As Outlook.Items = oInbox.Items
        'Console.WriteLine("Total : " & oItems.Count)

        ' Get unread e-mail messages.
        oItems = oItems.Restrict("[Unread] = true")
        'MsgBox("Total Unread : " & oItems.Count)

        ' Loop each unread message.
        Dim oMsg As Outlook.MailItem
        Dim i As Integer

        For i = 1 To oItems.Count
            oMsg = oItems.Item(i)
            SAPI.Speak("New helpdesk mail from " + oMsg.SenderName)
            'Console.WriteLine(i)
            'Console.WriteLine(oMsg.SenderName)
            'Console.WriteLine(oMsg.Subject)
        Next

        ' Log off.
        oNS.Logoff()

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oItems = Nothing
        oMsg = Nothing
        tmrCallCheck.Start()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub tmrCallCheck_Tick(sender As System.Object, e As System.EventArgs) Handles tmrCallCheck.Tick
        tmrCallCheck.Stop()
        ' Create Outlook application.
        Dim oApp2 As Outlook.Application = New Outlook.Application()
        Dim SAPI
        SAPI = CreateObject("SAPI.spvoice")

        ' Get Mapi NameSpace.
        Dim oNS2 As Outlook.NameSpace = oApp2.GetNamespace("mapi")
        oNS2.Logon("Outlook", Missing.Value, False, True) ' TODO:

        ' Get Messages collection of Inbox.
        Dim oInbox2 As Outlook.MAPIFolder = oNS2.Folders("Facilities Helpdesk").Folders("Calls")
        Dim oItems2 As Outlook.Items = oInbox2.Items
        'Console.WriteLine("Total : " & oItems.Count)

        ' Get unread e-mail messages.
        oItems2 = oItems2.Restrict("[Unread] = true")
        'MsgBox("Total Unread : " & oItems.Count)

        ' Loop each unread message.
        Dim oMsg2 As Outlook.MailItem
        Dim j As Integer

        For j = 1 To oItems2.Count
            oMsg2 = oItems2.Item(j)
            SAPI.Speak("Missed call notification from " + oMsg2.SenderName)
            'Console.WriteLine(i)
            'Console.WriteLine(oMsg.SenderName)
            'Console.WriteLine(oMsg.Subject)
        Next

        ' Log off.
        oNS2.Logoff()

        ' Clean up.
        oApp2 = Nothing
        oNS2 = Nothing
        oItems2 = Nothing
        oMsg2 = Nothing
    End Sub
End Class
